package com.gem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gem.domain.CoreEvent;
import com.gem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-22 19:38
 * @Modified By:
 */
@Controller
@ResponseBody
@SuppressWarnings("all")
public class EchartsController {

    @Autowired
    private CoreService coreService;

    @Autowired
    private CampusService campusService;

    @Autowired
    private CampusVisitService campusVisitService;

    @Autowired
    private UserService userService;

    @Autowired
    private TrainingOrderService orderService;

    @RequestMapping("/echarts/campus")
    public String data(Map<String, Integer> map) {
        //查询所有渠道专员
        List<Integer> allSellers = campusService.getAllSellers();
        for (Integer uid : allSellers) {
            //查询每个渠道专员下负责的院校核心事件完成数
            System.out.println(uid);
            String name = userService.findById(uid).getPersonname();
            Integer count = coreService.countEvent(uid);
            map.put(name, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(json);
        return json;
    }

    @RequestMapping("/echarts/campusWeek")
    public String dataWeek(Map<String, Integer> map) {
        //查询所有渠道专员
        List<Integer> allSellers = campusService.getAllSellers();
        System.out.println(allSellers);
        for (Integer uid : allSellers) {
            //查询每个渠道专员下负责的院校核心事件完成数
            System.out.println(uid);
            String name = userService.findById(uid).getPersonname();
            Integer count = coreService.countEventWeek(uid);
            map.put(name, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println("week:---"+json);
        return json;
    }

    @RequestMapping("/echarts/leader")
    public String dataLeader(Map<String, Integer> map) {
        return getJson(map, "院校领导来访");
    }
    @RequestMapping("/echarts/leaderWeek")
    public String dataLeaderWeek(Map<String, Integer> map) {
        return getJson(map, "院校领导来访");
    }

    @RequestMapping("/echarts/student")
    public String dataStudent(Map<String, Integer> map) {
        return getJson(map, "学生代表来访");
    }
    @RequestMapping("/echarts/studentWeek")
    public String dataStudentWeek(Map<String, Integer> map) {
        return getJson(map, "学生代表来访");
    }

    @RequestMapping("/echarts/course")
    public String dataCourse(Map<String, Integer> map) {
        return getJson(map, "课程嵌入");
    }
    @RequestMapping("/echarts/courseWeek")
    public String dataCourseWeek(Map<String, Integer> map) {
        return getJson(map, "课程嵌入");
    }

    @RequestMapping("/echarts/lecture")
    public String dataLecture(Map<String, Integer> map) {
        return getJson(map, "招生讲座");
    }
    @RequestMapping("/echarts/lectureWeek")
    public String dataLectureWeek(Map<String, Integer> map) {
        return getJson(map, "招生讲座");
    }

    @RequestMapping("/echarts/qqwei")
    public String dataQqwei(Map<String, Integer> map) {
        return getJson(map, "建立QQ/微信群");
    }
    @RequestMapping("/echarts/qqweiWeek")
    public String dataQqweiWeek(Map<String, Integer> map) {
        return getJson(map, "建立QQ/微信群");
    }

    @RequestMapping("/echarts/winter")
    public String dataWinter(Map<String, Integer> map) {
        return getJson(map, "冬令营");
    }
    @RequestMapping("/echarts/winterWeek")
    public String dataWinterWeek(Map<String, Integer> map) {
        return getJson(map, "冬令营");
    }

    @RequestMapping("/echarts/summer")
    public String dataSummer(Map<String, Integer> map) {
        return getJson(map, "夏令营");
    }
    @RequestMapping("/echarts/summerWeek")
    public String dataSummerWeek(Map<String, Integer> map) {
        return getJson(map, "夏令营");
    }


    private String getJson(Map<String, Integer> map, String eventName) {
        //查询所有渠道专员
        List<Integer> allSellers = campusService.getAllSellers();
        for (Integer uid : allSellers) {
            //查询每个渠道专员下负责的院校核心事件完成数
            String name = userService.findById(uid).getPersonname();
            Integer count = coreService.countName(uid, eventName);
            map.put(name, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    private String getJsonWeek(Map<String, Integer> map, String eventName) {
        //查询所有渠道专员
        List<Integer> allSellers = campusService.getAllSellers();
        for (Integer uid : allSellers) {
            //查询每个渠道专员下负责的院校核心事件完成数
            String name = userService.findById(uid).getPersonname();
            Integer count = coreService.countNameWeek(uid, eventName);
            map.put(name, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }



    //所负责的院校累计交往次数
    @RequestMapping("/echarts/visit")
    public String dataGemPerson(Map<String, Integer> map) {
        //查询所有我方人员
        List<Integer> gemPersons = campusVisitService.findGemPersons();
        for (Integer gemPerson : gemPersons) {
            //查询每个渠道专员下负责的院校
            Integer count = campusVisitService.findByGemPerson(gemPerson);
            String name = userService.findById(gemPerson).getPersonname();
            map.put(name, count);
        }
        System.out.println(map);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    //所负责的院校本周交往次数
    @RequestMapping("/echarts/visitWeek")
    public String dataGemPersonWeek(Map<String, Integer> map) {
        //查询所有我方人员
        List<Integer> gemPersons = campusVisitService.findGemPersons();
        for (Integer gemPerson : gemPersons) {
            //查询每个渠道专员下负责的院校
            Integer count = campusVisitService.findByGemPersonWeek(gemPerson);
            String name = userService.findById(gemPerson).getPersonname();
            map.put(name, count);
        }
        System.out.println(map);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    //累计交往次数
    @RequestMapping("/echarts/visitCampus")
    public String dataCampus(Map<String, Integer> map) {
        //查询所有院校
        List<String> campusList = campusVisitService.findCampus();
        for (String campus : campusList) {
            //查询每个渠道专员下负责的院校
            Integer count = campusVisitService.findByCampus(campus);
            map.put(campus, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    //本周交往次数
    @RequestMapping("/echarts/visitCampusWeek")
    public String dataCampusWeek(Map<String, Integer> map) {
        //查询所有院校
        List<String> campusList = campusVisitService.findCampus();
        for (String campus : campusList) {
            //查询每个渠道专员下负责的院校
            Integer count = campusVisitService.findByCampusWeek(campus);
            map.put(campus, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }


    //累计招生人数
    @RequestMapping("/echarts/EnrollmentNum")
    public String EnrollmentNum(Map<String, Integer> map) {
        //查询所有渠道专员
        List<Integer> campusNames = orderService.findMid();
        for (Integer name : campusNames) {
            //查询每个渠道专员下负责的院校
            Integer count = orderService.countStu(name);
            map.put(userService.findById(name).getPersonname(), count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    //本周新增招生人数
    @RequestMapping("/echarts/EnrollmentNumWeek")
    public String EnrollmentNumWeek(Map<String, Integer> map) {
        //查询所有渠道专员
        List<Integer> campusNames = orderService.findMid();
        for (Integer name : campusNames) {
            //查询每个渠道专员下负责的院校
            Integer count = orderService.countStuWeek(name);
            map.put(userService.findById(name).getPersonname(), count);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}
